package com.hyx.api.service.impl.pc_admin;

import java.util.List;
import java.util.Arrays;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.hyx.api.mapper.SpreadOrderMapper;
import com.hyx.api.domain.SpreadOrder;
import com.hyx.api.domain.bo.SpreadOrderBo;
import com.hyx.api.domain.vo.SpreadOrderVo;
import com.hyx.api.service.pc_admin.ISpreadOrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;

/**
 * 追差订单Service业务层处理
 *
 * @author HK
 * @date 2023-10-26
 */
@RequiredArgsConstructor
@Service
public class SpreadOrderServiceImpl implements ISpreadOrderService
{
    private final SpreadOrderMapper spreadOrderMapper;

    /**
     * 查询追差订单
     *
     * @param id 追差订单主键
     * @return 追差订单
     */
    @Override
    public SpreadOrderVo selectSpreadOrderById(Long id)
    {
        //TODO 表以外的字段，写入vo
        return spreadOrderMapper.selectVoById(id);
    }

    /**
     * 查询追差订单列表
     *
     * @param bo 追差订单
     * @return 追差订单
     */
    @Override
    public List<SpreadOrderVo> selectSpreadOrderList(SpreadOrderBo bo)
    {
        LambdaQueryWrapper<SpreadOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtils.isNotEmpty(bo.getRecycleOrderId()), SpreadOrder::getRecycleOrderId, bo.getRecycleOrderId());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getSpread()), SpreadOrder::getSpread, bo.getSpread());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getOperatorId()), SpreadOrder::getOperatorId, bo.getOperatorId());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getPayImg()), SpreadOrder::getPayImg, bo.getPayImg());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getStatus()), SpreadOrder::getStatus, bo.getStatus());
        return spreadOrderMapper.selectVoList(lqw);
    }

    /**
     * 新增追差订单
     *
     * @param bo 追差订单
     * @return 结果
     */
    @Override
    public int insertSpreadOrder(SpreadOrderBo bo)
    {
        SpreadOrder spreadOrder = tranEntityBeforeSave(bo);
        validEntityBeforeSave(spreadOrder);
        return spreadOrderMapper.insert(spreadOrder);
    }

    /**
     * 修改追差订单
     *
     * @param bo 追差订单
     * @return 结果
     */
    @Override
    public int updateSpreadOrder(SpreadOrderBo bo)
    {
        SpreadOrder spreadOrder = tranEntityBeforeSave(bo);
        return spreadOrderMapper.updateById(spreadOrder);
    }

    /**
     * 批量删除追差订单
     *
     * @param ids 需要删除的追差订单主键
     * @return 结果
     */
    @Override
    public int deleteSpreadOrderByIds(Long[] ids)
    {
        return spreadOrderMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SpreadOrder entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     *   保存前的数据转换
     */
    private SpreadOrder tranEntityBeforeSave(SpreadOrderBo bo) {
        //TODO 做一些数据转换
        SpreadOrder spreadOrder = new SpreadOrder();
        BeanUtils.copyProperties(bo, spreadOrder);
        return spreadOrder;
    }
}

